import requests
import re

#1、超时
base_url = r'http://httpbin.org/'
url=base_url+'get'
reap=requests.get(url,timeout=3)# timeout:指定超时时间
# resp=requests.get(url,timeout=(3,5))# 3:connect超时，5：read超时
print(reap.status_code)
# print(resp.text)

#2、SSL验证
#访问12306页面
url=r'https://www.12306.cn/index/'
reap=requests.get(url,verify=False)# 开启Fiddler的情况下，需要设置 verify
reap.encoding='utf-8'
patt=r'<title>(.*?)</title>'#获取title值
title=re.findall(patt,reap.text)
print(title)

from requests.auth import HTTPBasicAuth
#3、身份认证
# 基本身份认证：访问 tomcat 主机管理页
url = r'http://172.16.22.104:8080/host-manager/html'

#方法1
# auth=HTTPBasicAuth('tomcat','tomcat')
# resp=requests.get(url,auth=auth)
# print(resp.text)

# 方法2
resp = requests.get(url, auth=('tomcat', 'tomcat'))
print(resp.text)

# 摘要式身份认证
from requests.auth import HTTPDigestAuth

url = r'https://www.httpbin.org/digest-auth/auth/user/passwd'
auth = HTTPDigestAuth('user', 'passwd')
resp = requests.get(url, auth=auth)
print(resp.status_code)
print(resp.text)